﻿using Splendor_Server.script.logic.player;
using Splendor_Server.script.logic.room;
using Splendor_Server.script.net;

namespace Splendor_Server.script.logic.msg
{
    public partial class MsgHandler
    {
        //请求房间列表
        public static void MsgGetRoomList(ClientState client, MsgBase msgBase)
        {
            MsgGetRoomList msg = (MsgGetRoomList) msgBase;
            Player player = client.player;
            if (player == null) return;
            msg.rooms = RoomManager.ToRoomInfoList();
            player.Send(msg);
        }

        //创建房间
        public static void MsgCreateRoom(ClientState client, MsgBase msgBase)
        {
            MsgCreateRoom msg = (MsgCreateRoom) msgBase;
            Player player = client.player;
            if (player == null) return;
            //已经在房间里
            if (player.IsInRoom())
            {
                msg.result = 0;
                player.Send(msg);
                return;
            }

            Room room = RoomManager.AddRoom(msg.isPrivate, msg.point, msg.rules);
            room.AddPlayer(player.GetPlayerId());

            msg.result = 1;
            player.Send(msg);
        }

        //进入房间
        public static void MsgEnterRoom(ClientState client, MsgBase msgBase)
        {
            MsgEnterRoom msg = (MsgEnterRoom) msgBase;
            Player player = client.player;
            if (player == null) return;
            //已经在房间里
            if (player.IsInRoom())
            {
                msg.result = (int) EnterRoomResult.IsInRoom;
                player.Send(msg);
                return;
            }

            Room room = RoomManager.GetRoom(msg.rId);
            if (room == null)
            {
                msg.result = (int) EnterRoomResult.Nonexistence;
                player.Send(msg);
                return;
            }

            //进入
            if (!room.AddPlayer(player.GetPlayerId()))
            {
                msg.result = (int) EnterRoomResult.Other;
                player.Send(msg);
                return;
            }

            //返回协议	
            msg.result = (int) EnterRoomResult.Successful;
            msg.pInfo = player.ToPlayerInfo();
            room.Broadcast(msg);
        }

        //获取房间信息
        public static void MsgGetRoomInfo(ClientState client, MsgBase msgBase)
        {
            MsgGetRoomInfo msg = (MsgGetRoomInfo) msgBase;
            Player player = client.player;
            if (player == null) return;

            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null)
            {
                player.Send(msg);
                return;
            }

            player.Send(room.ToGetRoomInfo());
        }

        //离开房间/战斗
        public static void MsgLeaveRoom(ClientState client, MsgBase msgBase)
        {
            MsgLeaveRoom msg = (MsgLeaveRoom) msgBase;
            Player player = client.player;
            if (player == null) return;

            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null)
            {
                msg.result = 0;
                player.Send(msg);
                return;
            }

            if (!room.RemovePlayer(player.GetPlayerId()))
            {
                msg.result = 0;
                player.Send(msg);
            }

            msg.result = 1;
            msg.pId = player.GetPlayerId();
            player.Send(msg);
        }

        public static void MsgStartBattle(ClientState client, MsgBase msgBase)
        {
            MsgStartBattle msg = (MsgStartBattle) msgBase;
            Player player = client.player;
            if (player == null) return;

            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null)
            {
                msg.result = (int) StartBattleResult.Nonexistence;
                player.Send(msg);
                return;
            }

            if (!room.IsOwner(player))
            {
                msg.result = (int) StartBattleResult.NotOwner;
                player.Send(msg);
                return;
            }

            msg.result = (int) room.CheckStartBattle();
            player.Send(msg);
        }

        public static void MsgPrepareBattle(ClientState client, MsgBase msgBase)
        {
            MsgPrepareBattle msg = (MsgPrepareBattle) msgBase;
            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null)
            {
                msg.result = (int) StartBattleResult.Nonexistence;
                player.Send(msg);
                return;
            }

            bool isPrepared = !player.IsPrepared();
            player.SetPrepare(isPrepared);
            msg.result = isPrepared ? 1 : 0;
            msg.pId = player.GetPlayerId();
            room.Broadcast(msg);
        }
    }
}